using Microsoft.EntityFrameworkCore;
using OneCaps.Core.Entity;
using OneCaps.Core.Enum;
using OneCaps.Core.Interfaces;

namespace OneCaps.DB
{
    /// <summary>
    /// 车辆仓库实现
    /// </summary>
    public class VehicleRepository : EFCoreRepository<Vehicle, int>, IVehicleRepository
    {
        public VehicleRepository(AppDbContext context) : base(context)
        {
        }

        public async Task<Vehicle?> GetByLicensePlateAsync(string licensePlate)
        {
            return await _dbSet
                .Include(v => v.CurrentParkingRecord)
                .FirstOrDefaultAsync(v => v.LicensePlate == licensePlate);
        }

        public async Task<bool> ExistsByLicensePlateAsync(string licensePlate)
        {
            return await _dbSet.AnyAsync(v => v.LicensePlate == licensePlate);
        }

        public async Task<IEnumerable<Vehicle>> GetParkingVehiclesAsync()
        {
            return await _dbSet
                .Include(v => v.CurrentParkingRecord)
                .Where(v => v.CurrentParkingRecord != null && v.CurrentParkingRecord.Status == ParkingRecordStatus.Parking)
                .ToListAsync();
        }

        public async Task<IEnumerable<Vehicle>> GetByVehicleTypeAsync(VehicleType vehicleType)
        {
            return await _dbSet
                .Where(v => v.Type == vehicleType)
                .ToListAsync();
        }

        public async Task<IEnumerable<Vehicle>> GetVipVehiclesAsync()
        {
            return await _dbSet
                .Where(v => v.IsVip)
                .ToListAsync();
        }

        public async Task<IEnumerable<Vehicle>> SearchVehiclesAsync(string keyword)
        {
            if (string.IsNullOrWhiteSpace(keyword))
            {
                return await GetAllAsync();
            }

            return await _dbSet
                .Where(v => v.LicensePlate.Contains(keyword) ||
                           (v.OwnerName != null && v.OwnerName.Contains(keyword)) ||
                           (v.Brand != null && v.Brand.Contains(keyword)) ||
                           (v.Model != null && v.Model.Contains(keyword)))
                .ToListAsync();
        }
    }
} 